﻿using PeiYue.Common;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Store;
using PeiYue.Model.Dto.Store;
using PeiYue.Model.ERPModels;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Store
{
    public class SaleDeliveryHeaderServices : BaseServices<SaleDeliveryHeader>, ISaleDeliveryHeaderServices
    {
        IBaseRepository<SaleDeliveryHeader> _dal;
        public SaleDeliveryHeaderServices(IBaseRepository<SaleDeliveryHeader> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }
        public async Task<List<DeliveryRecordDto>> GetDeliverySkuList(string customerCode)
        {
            List<SugarParameter> parameters = new List<SugarParameter>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("  select * from ( ");
            strSql.Append("select   ROW_NUMBER()  over( partition by j.条码  order by isnull(dr.Status,0)  )RowNum, ");
            strSql.Append(" d.*,isnull(j.包裹数,1) as PackQty,isnull(j.数量,0) as SkuQty,j.条码 as BarCode,dr.Status ");
            strSql.Append(" from( ");
            strSql.Append("select d.销售ID as OrderDetailId,d.品名 as SkuName,d.单位 as SkuSpec,(isnull(h.优先级,'')+h.客户编号) as CustomerCode ");
            strSql.Append(" from v销售发货表体 d ");
            strSql.Append("  inner join v销售发货表头 h on d.单号 = h.单号 ");
            strSql.Append(" where h.客户编号 = @customerCode and h.日期 >= @today ");
            strSql.Append(" and h.类型 = '正常订单'  ");
            strSql.Append("  ) d ");
            strSql.Append("  left join v仓库拣选日志 j on d.OrderDetailId = j.销售ID ");
            strSql.Append("  left join OMS.[dbo].[DeliveryRecord] dr on j.条码 = dr.BarCode ");
            strSql.Append("  where  j.数量>0  ");
            strSql.Append(" )t where (t.RowNum=1 and Status=1) or (RowNum=2 and Status!=1)   or t.Status is null order by BarCode ");
            parameters.Add(new SugarParameter("@customerCode", "A" + customerCode));
            DateTime date = (DateTime.Now.Hour >= int.Parse(Appsettings.app(new string[] { "QueryHour", "ReceiptHour" })) ? DateTime.Now.AddDays(1).Date : DateTime.Now.Date);
            parameters.Add(new SugarParameter("@today", date));
            return await base.SqlQueryList<DeliveryRecordDto>(strSql.ToString(), parameters);


        }


        public async Task<List<DeliveryRecordDto>> GetDeliveryList(DeliveryDto query)
        {//,h.优先级 CustomerPriority
            List<SugarParameter> parameters = new List<SugarParameter>();
            StringBuilder strSql = new StringBuilder();
            string upDate =DateTime.Now.AddDays(-1).ToShortDateString();
            //string date = "2020/11/1";
            string date = (DateTime.Now.Hour >= int.Parse(Appsettings.app(new string[] { "QueryHour", "ReceiptHour" })) ? DateTime.Now.AddDays(1).ToShortDateString() : DateTime.Now.ToShortDateString());
            strSql.Append($@"  SELECT dr.Id ,d.销售ID AS OrderDetailId ,d.品名 AS SkuName ,d.单位 AS SkuSpec , (isnull(h.优先级, '') + h.客户编号)  CustomerCode,(case when isnull(j.数量,0)=0 then d.数量 else j.数量 end) SkuQty,j.条码 BarCode,客户ID CusId ,u.namec DeliveryUser,dr.CreateTime DeliveryTime,d.行号 as LineNumber 
FROM v销售发货表体 d
INNER JOIN v销售发货表头 h ON d.单号 = h.单号
LEFT JOIN v仓库拣选日志 j ON d.销售ID = j.销售ID
LEFT JOIN  (select Id ,BarCode,UpdateUser,CreateTime from OMS.[dbo].[DeliveryRecord]where Status=0 and CreateTime>'{upDate}' ) dr ON j.条码 = dr.BarCode
left join  [OMS].[dbo].[oms_ur_user] u on u.id=dr.UpdateUser
WHERE h.日期 >='{date}'   AND h.类型 = '正常订单'  ");
            if (!string.IsNullOrEmpty(query.CustomerCode))
            {
                strSql.Append("  AND h.客户编号 = @customerCode ");
                parameters.Add(new SugarParameter("@customerCode", query.CustomerCode.Replace("B", "")));
            }
            strSql.Append("  order by d.行号  ");
            return await base.SqlQueryList<DeliveryRecordDto>(strSql.ToString(), parameters);
        }

    }
}
